package com.rst.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import com.rst.model.Section;
import com.rst.model.Subject;
import com.rst.service.SectionService;
import com.rst.service.SubjectService;


@Controller
public class SubjectController {
	
	@Autowired
	SectionService sectionService;
	
	@Autowired
	SubjectService subjectService;

	@RequestMapping(value="/addSubject", method=RequestMethod.GET)
	public ModelAndView addsubject()
	{
		ModelAndView mav = new ModelAndView("subject/addSubject");
		Subject subject = new Subject();
		List<Section> sectionList = sectionService.getSectionList();
		mav.addObject("sectionList", sectionList);
		mav.getModelMap().put("subject", subject);
		return mav;
	}
	
	@RequestMapping(value="/saveSubject", method=RequestMethod.POST)
	public String saveSubject(@ModelAttribute("subject")Subject subject, BindingResult result, SessionStatus status)
	{
		subjectService.saveSubject(subject);
		status.setComplete();
		return "redirect:managesubject.htm" ;
	}
	
	@RequestMapping(value="/editSubject", method=RequestMethod.GET)
	public ModelAndView editWeekday(@RequestParam("id")Long subjectId)
	{
		ModelAndView mav = new ModelAndView("subject/editSubject");
		Subject subject = subjectService.getSubjectBySubjectId(subjectId);
		List<Section> sectionList = sectionService.getSectionList();
		mav.addObject("sectionList", sectionList);
		mav.addObject("subject", subject);
		return mav;
	}
	

	@RequestMapping(value="/ajax/getSubjectListAjax", method=RequestMethod.GET)
	public @ResponseBody String getSubjectListAjax(@RequestParam("id")Long sectionId)
	{
		List<Subject> subjectList = subjectService.getSubjectListBySectionId(sectionId);
		String ajax = subjectService.processSubjectList(subjectList);
		return ajax;
	}
	
	@RequestMapping(value="/managesubject", method=RequestMethod.GET)
	public ModelAndView manageSubject()
	{
		ModelAndView mav = new ModelAndView("subject/manageSubject");
		List<Section> sectionList = sectionService.getSectionList();
		mav.addObject("sectionList", sectionList);
		return mav;
	}
	
	@RequestMapping(value="/deleteSubject", method=RequestMethod.GET)
	public String deleteSubject(@RequestParam("id")Long subjectId )
	{
		String status = subjectService.deleteSubject(subjectId);
		return "redirect:managesubject.htm" ;
	}
}
